<script>

import { PAGE_INFO } from '@/utils/MyUtils'
import VipExam from '@/views/admin/vip/VipExam.vue'
import VipDetail from '@/views/admin/vip/VipDetail.vue'
import BlogList from '@/views/admin/blog/BlogList.vue'

export default {
  name: 'ViewList',
  components: { BlogList, VipDetail, VipExam },
  data () {
    return {
      pageInfo: {
        ...PAGE_INFO
      },
      searchInfo: {
        email: '',
        idcard: '',
        nickname: '',
        phone: '',
        username: '',
        status: '',
        userinfoStatus: ''
      },
      tableList: [],
      delIds: [],
      showBlog: false
    }
  },
  methods: {
    getTableList () {
      this.$axios.get('vip/page', { params: { ...this.pageInfo, ...this.searchInfo } }).then(data => {
        this.tableList = [...data.records]
        this.pageInfo.count = data.total
      })
    },
    currentChange (value) {
      this.pageInfo.page = value
      this.getTableList()
    },
    sizeChange (value) {
      this.pageInfo.page = 1
      this.pageInfo.limit = value
      this.getTableList()
    },
    search () {
      this.pageInfo.page = 1
      this.getTableList()
    },
    clear () {
      this.pageInfo = { ...this.$options.data().pageInfo }
      this.searchInfo = { ...this.$options.data().searchInfo }
      this.getTableList()
    },
    statusChange (userinfoId) {
      this.$confirm(`确定更改编号【${userinfoId}】的状态吗?`, { type: 'info', title: '询问' }).then(() => {
        this.$axios.post('vip/status/' + userinfoId).then(() => {
          this.$message.success('操作成功！')
          this.getTableList()
        }).catch(e => e)
      }).catch(e => e)
    },
    deleteData (userinfoId) {
      if (!userinfoId && this.delIds.length === 0) {
        this.$alert('请至少选中1条进行删除！', { type: 'warning' })
        return false
      }
      const deleteIds = userinfoId ? [userinfoId] : [...this.delIds]
      this.$confirm(`确定删除[${deleteIds}]的数据吗?`, { type: 'info', title: '询问' }).then(() => {
        this.$axios.delete('vip/delete', { data: deleteIds }).then(() => {
          this.$message.success('操作成功！')
          this.getTableList()
        }).catch(e => e)
      }).catch(e => e)
    },
    selectionChange (selection) {
      this.delIds = [...selection.map(item => item.userinfoId)]
    },
    exam (userinfoId) {
      this.$refs.vipExamRef.init(userinfoId)
    },
    detail (userinfoId) {
      this.$refs.vipDetailRef.init(userinfoId)
    },
    blogList (userinfoId) {
      this.showBlog = true
      console.log(this.$refs.blogListRef)
    }
  },
  created () {
    this.$nextTick(() => {
      this.getTableList()
    })
  }
}
</script>

<template>
  <div>
    <el-card>
      <el-form :model="searchInfo" inline label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="searchInfo.username" placeholder="用户名" clearable/>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="searchInfo.nickname" placeholder="昵称" clearable/>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="searchInfo.phone" placeholder="手机号" clearable/>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="searchInfo.email" placeholder="邮箱" clearable/>
        </el-form-item>
        <el-form-item label="身份证">
          <el-input v-model="searchInfo.idcard" placeholder="身份证号码" clearable/>
        </el-form-item>
        <el-form-item label="用户状态">
          <el-select v-model="searchInfo.status" placeholder="选择状态" clearable>
            <el-option :value="1" label="可用" />
            <el-option :value="2" label="禁用" />
          </el-select>
        </el-form-item>
        <el-form-item label="认证状态">
          <el-select v-model="searchInfo.userinfoStatus" placeholder="选择状态" clearable>
            <el-option :value="1" label="注册" />
            <el-option :value="2" label="提交" />
            <el-option :value="3" label="认证" />
            <el-option :value="4" label="拒绝" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="search">查询</el-button>
          <el-button @click="clear">清空</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-button @click="deleteData()">删除</el-button>
      <el-table @selection-change="selectionChange" :data="tableList" border>
        <el-table-column type="selection" fixed="left"/>
        <el-table-column prop="userinfoId" label="编号"/>
        <el-table-column prop="username" label="用户名"/>
        <el-table-column prop="nickname" label="昵称"/>
        <el-table-column prop="phone" label="手机号"/>
        <el-table-column prop="email" label="邮箱"/>
        <el-table-column prop="idcard" label="身份证号"/>
        <el-table-column label="认证状态">
          <template slot-scope="{row}">
            <el-tag v-if="row.userinfoStatus === 1" type="warning">注册</el-tag>
            <template v-else-if="row.userinfoStatus === 2">
              <el-tag type="primary">提交</el-tag>
              <el-button @click="exam(row.userinfoId)" type="text" title="点击审核"><i style="margin-left: 10px" class="el-icon-coordinate"></i></el-button>
            </template>
            <el-tag v-else-if="row.userinfoStatus === 3" type="success">认证</el-tag>
            <el-tag v-else type="danger">拒绝</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="数据状态">
          <template slot-scope="{row}">
            <el-button @click="statusChange(row.userinfoId)" title="点击切换状态" type="success" v-if="row.status === 1">可用</el-button>
            <el-button @click="statusChange(row.userinfoId)" title="点击切换状态" type="danger" v-else>禁用</el-button>
          </template>
        </el-table-column>
        <el-table-column label="数据" width="270px">
          <template slot-scope="{row}">
            <el-button @click="blogList(row.userinfoId)">博客</el-button>
            <el-button>分类</el-button>
            <el-button>标签</el-button>
          </template>
        </el-table-column>
        <el-table-column fixed="right" width="180px" label="操作">
          <template slot-scope="{row}">
            <el-button @click="deleteData(row.userinfoId)">删除</el-button>
            <el-button @click="detail(row.userinfoId)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page="pageInfo.page" :page-size="pageInfo.limit" :page-sizes="[5,10,20,50,100]" :total="pageInfo.count" layout="total,prev,pager,next,jumper,sizes" @current-change="currentChange" @size-change="sizeChange"/>
    </el-card>
    <vip-exam ref="vipExamRef" @close="getTableList"/>
    <vip-detail ref="vipDetailRef"/>
    <el-dialog width="80%" :visible.sync="showBlog" title="博客列表">
      <blog-list ref="blogListRef"/>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">

</style>
